﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Diagnostics;
using System.Xml.Linq;

namespace BaseTypesLib.Model
{
    [DataContract(IsReference = true)]
    public class ObjectChoiceField:ChoiceField
    {
        [DataMember]
        private ObservableCollection<string> choices;

        public ObjectChoiceField()
        {
            choices = new ObservableCollection<string>();
            choices.CollectionChanged += new NotifyCollectionChangedEventHandler(choices_CollectionChanged);
        }

        void choices_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            Debug.WriteLine(Choices.Count);
          //  OnPropertyChanged("Choices");
        }

        public ObservableCollection<string> Choices
        {
            get { return choices; }
        }
        protected override XElement GetXmlElement()
        {
            XElement el = base.GetXmlElement();

            foreach (string s in Choices)
            {
                XElement item = new XElement("Choice");
                item.SetAttributeValue("Value", s);
                el.Add(item);
            }
            return el;
        }
        protected override void FromXmlElement(XElement elem)
        {
            base.FromXmlElement(elem);
            var choices = elem.Elements("Choice").ToList();
            foreach (XElement ch in choices)
            {
                var attr = ch.Attributes("Value").FirstOrDefault();
                if (attr != null)
                    Choices.Add(attr.Value);
            }
        }
    }
}
